home *** CD-ROM | disk | FTP | other *** search
/ The Complete Windows Set / The Complete Windows Set.iso / CODE / WCDBRK2.C < prev    next >
C/C++ Source or Header  |  1992-04-22  |  17KB  |  681 lines

  1. /*********************************************************************
  2.     PROGRAM: WCDBRK2.C
  3.  
  4.     PURPOSE: Code Break game
  5.  
  6.     AUTHOR:  Ken Fogel
  7.          Omnibus Systems
  8.          8108 Norfolk Road
  9.          Cote St-Luc, Québec
  10.          Canada   H4X 1A3
  11.          (514) 487-1565
  12.          CompuServe: 74646,2157
  13.  
  14.     REVISION: April 22, 1992 - Version 2.1
  15.                               Corrected a bug when multiple blue
  16.                                  pegs are in the secret code
  17.  
  18.              June 1, 1991 - Version 2
  19.                                      Full Colour/Bitmaps
  20.  
  21.                  April 22, 1991 - Version 1.01
  22.                  April 15, 1991 - Version 1
  23.  
  24. *********************************************************************/
  25.  
  26. #include <windows.h>           /* required for all Windows applications */
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <time.h>
  31. #include "wcdbrk2.h"        /* specific to this program */
  32.  
  33. HANDLE  hInst;                  /* current instance */
  34. HWND      hGameGrid[10][4];   /* array of buttons */
  35. int      board[10][4];        /* array of guesses */
  36. int      code[4];              /* secret code */
  37. char      clues[10][4];        /* array of clues */
  38. int      cur_round;           /* current row */
  39. int     cur_column;         /* current column */
  40. char      str[80];              /* general purpose string */
  41. HWND    DidIGetItRight;
  42. BOOL    Setup;
  43. BITMAP  Bitmap;
  44. HBITMAP hOldBitmap;
  45. HBITMAP hBoardmap;
  46. HBITMAP hBlclpegmap;
  47. HBITMAP hWhclpegmap;
  48. HBITMAP hPegBoardmap;
  49. LPSTR   lpPegs[8] = {"Bluepeg","Dkpupeg","Grenpeg","Ltblpeg",
  50.                              "Oranpeg","Purppeg","Redpeg","Yellpeg"};
  51. HBITMAP hPegs[8];
  52. int iXpos,iYpos;
  53. int curcolour = 0;
  54. int prevcolour = -1;
  55. int winstate;
  56.  
  57. /*********************************************************************/
  58.  
  59. int PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  60. HANDLE hInstance;                /* current instance */
  61. HANDLE hPrevInstance;         /* previous instance */
  62. LPSTR lpCmdLine;                /* command line */
  63. int nCmdShow;                    /* show-window type (open/icon) */
  64. {
  65.     MSG msg;                       /* message */
  66.  
  67.     Setup = FALSE;
  68.  
  69.     if (!hPrevInstance)                       /* Other instances of app running? */
  70.         if (!InitApplication(hInstance))      /* Initialize shared things */
  71.             return (FALSE);                   /* Exits if unable to initialize */
  72.  
  73.     /* Perform initializations that apply to a specific instance */
  74.  
  75.     if (!InitInstance(hInstance, nCmdShow))
  76.         return (FALSE);
  77.  
  78.  
  79.     /* Aquire and dispatch messages until a WM_QUIT message is recieved */
  80.  
  81.     while (GetMessage(&msg,                  /* message structure */
  82.                             NULL,     /* handle of window recieving the message */
  83.                             NULL,     /* lowest message to examine */
  84.                             NULL))     /* highest message to examine */
  85.     {
  86.         TranslateMessage(&msg);     /* Translates virtual key codes */
  87.         DispatchMessage(&msg);     /* Dispatches message to window */
  88.     }
  89.     return(msg.wParam);  /* Returns the value from PostQuitMessage */
  90. }
  91.  
  92. /*********************************************************************/
  93.  
  94. BOOL InitApplication(hInstance)
  95. HANDLE hInstance;              /* current instance */
  96. {
  97.     WNDCLASS wc;
  98.  
  99.     /* Fill in window class structure with parameters that describe the */
  100.     /* main window. */
  101.  
  102.     wc.style = CS_DBLCLKS;   /* Class style(s). */
  103.     wc.lpfnWndProc = MainWndProc; /* Function to retrieve messages for */
  104.                                             /* windows of this class */
  105.     wc.cbClsExtra = 0;              /* No per-class extra data */
  106.     wc.cbWndExtra = 0;              /* No per-window extra data */
  107.     wc.hInstance = hInstance;       /* Application that owns the class. */
  108.     wc.hIcon = LoadIcon(hInstance, "Wcdbrk2");
  109.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  110.     wc.hbrBackground = GetStockObject(WHITE_BRUSH);
  111.     wc.lpszMenuName = "WndCdBrkMenu";   /* Name of menu resource in .RC file */
  112.     wc.lpszClassName = "WndCdBrkWClass";/* Name used in call to CreateWindow */
  113.  
  114.     /* Register the window class and return success/failure code */
  115.  
  116.     return (RegisterClass(&wc));
  117.  
  118. }
  119.  
  120. /*********************************************************************/
  121.  
  122. BOOL InitInstance(hInstance, nCmdShow)
  123. HANDLE hInstance;               /* Current instance identifier */
  124. int    nCmdShow;               /* Param for first ShowWindow() call */
  125. {
  126.     HWND hWnd;                   /* Main window handle */
  127.  
  128.     hInst = hInstance;
  129.  
  130.     /* Create a main window for this application instance */
  131.  
  132.     hWnd = CreateWindow(
  133.          "WndCdBrkWClass",         /* See RegisterClass() call */
  134.          "Code Breaker II",      /* Text for window title bar */
  135.          /* The following line creates a window which can be */
  136.          /* minimized to an icon and moved but not re-sized  */
  137.          WS_OVERLAPPED | WS_MINIMIZEBOX | WS_SYSMENU,
  138.          120,                      /* Initial column */
  139.          30,                       /* Initial row    */
  140.          230,                      /* Width          */
  141.          435,                      /* Height         */
  142.          NULL,                        /* Overlapped windows have no parent */
  143.          NULL,                        /* Use the window class menu */
  144.          hInstance,                    /* This instance owns this window */
  145.          NULL                           /* Pointer not needed */
  146.          );
  147.  
  148.     /* If window could not be created, return "failure" */
  149.  
  150.     if (!hWnd)
  151.         return (FALSE);
  152.  
  153.     /* Make the window visible; update its client area; and return "success" */
  154.  
  155.     ShowWindow(hWnd, nCmdShow);     /* Show the window */
  156.     UpdateWindow(hWnd);               /* Sends WM_PAINT message */
  157.  
  158.     return (TRUE);         /* Returns the value from POSTQUITMESSAGE */
  159.  
  160. }
  161.  
  162. /*********************************************************************/
  163.  
  164. long FAR PASCAL MainWndProc(hWnd, message, wParam, lParam)
  165. HWND hWnd;                      /* window handle */
  166. unsigned message;               /* type of message */
  167. WORD wParam;                   /* additional information */
  168. LONG lParam;                   /* additional information */
  169. {
  170.     FARPROC     lpProcAbout;           /* pointer to the "About" function */
  171.     int x,y;
  172.  
  173.  
  174.     switch (message)
  175.     {
  176.         case WM_CREATE:
  177.             if (!Setup)    /* Do this only once */
  178.             {
  179.                 hBoardmap   = LoadBitmap(hInst,"Board");
  180.                 hPegBoardmap = LoadBitmap(hInst,"Pegboard");
  181.                 hBlclpegmap = LoadBitmap(hInst,"Blclpeg");
  182.                 hWhclpegmap = LoadBitmap(hInst,"Whclpeg");
  183.                 for (x=0;x<8;x++)
  184.                     hPegs[x] = LoadBitmap(hInst,lpPegs[x]);
  185.                 if(!bMakeGameButtons(hWnd))
  186.                 {
  187.                     MessageBox(hWnd,"Failure to create buttons!",NULL,
  188.                                   MB_OK | MB_ICONHAND);
  189.                 }
  190.                 Setup = TRUE;
  191.             }
  192.             vInitialize();
  193.             vMakecode();
  194.             break;
  195.  
  196.         case WM_LBUTTONUP:          /* Mouse button was pressed */
  197.             iXpos = LOWORD(lParam);
  198.             iYpos = HIWORD(lParam);
  199.             if (bCheckPosition(hWnd))
  200.             {
  201.                 board[cur_round][cur_column] = curcolour;
  202.                 vPutPeg(hWnd,curcolour,cur_round,cur_column);
  203.             }
  204.             break;
  205.  
  206.         case WM_PAINT:              /* Time to repaint */
  207.             vBasicScreen(hWnd);
  208.             vMarkColour(hWnd);
  209.             for (x=0;x<=cur_round;x++)
  210.             {
  211.                 for (y=0;y<4;y++)
  212.                     vPutPeg(hWnd,board[x][y],x,y);
  213.                 vDoClues(hWnd,x);
  214.             }
  215.             if (winstate == 1)
  216.                 vDoWin(hWnd);
  217.             else
  218.                 if (winstate == 2)
  219.                     vDoLoose(hWnd);
  220.  
  221.             break;
  222.  
  223.  
  224.         case WM_COMMAND:         /* message: command from application menu */
  225.             switch(wParam)
  226.             {
  227.                 case DIDIGETITRIGHT:
  228.                     if (!winstate)    /* If won or lost ignore commands */
  229.                     {
  230.                         if (!bCheckfill()) break;
  231.                         if (bCheckguess(cur_round))
  232.                         {
  233.                             vDoWin(hWnd);
  234.                             break;
  235.                         }
  236.                         else
  237.                         {
  238.                             vDoClues(hWnd,cur_round);
  239.                             cur_round++;
  240.                             if (cur_round == 10) /* You loose */
  241.                                 vDoLoose(hWnd);
  242.                         }
  243.                     }
  244.                     break;
  245.  
  246.                 case IDM_ABOUT:
  247.                     lpProcAbout = MakeProcInstance(About, hInst);
  248.  
  249.                     DialogBox(hInst,            /* current instance */
  250.                                  "AboutBox",   /* resource to use */
  251.                                  hWnd,            /* parent handle */
  252.                                  lpProcAbout);    /* About() instance address */
  253.  
  254.                     FreeProcInstance(lpProcAbout);
  255.                     break;
  256.  
  257.                 case IDM_NEW:  /* Let's play again */
  258.                   InvalidateRect(hWnd, NULL, TRUE);
  259.                   SendMessage(hWnd,WM_CREATE,NULL,NULL);
  260.                   break;
  261.  
  262.                 case IDM_GIVEUP:
  263.                     vDoLoose(hWnd);
  264.                     break;
  265.  
  266.                 case IDM_HELP:
  267.                     lpProcAbout = MakeProcInstance(About, hInst);
  268.  
  269.                     DialogBox(hInst,            /* current instance */
  270.                                  "HelpBox",   /* resource to use */
  271.                                  hWnd,